package com.sgb.kjwl.viewmodel.transport;

import android.app.Application;
import android.content.Context;
import android.util.ArrayMap;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.NewAPIManager;
import com.sgb.kjwl.model.entity.transport.TeamRespVoEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.event.self.GoodsManageEvent;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;

import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FleetDetailsModel extends AndroidViewModel {
    public MutableLiveData<TeamRespVoEntity> ldlist = new MutableLiveData<>();
    public MutableLiveData<String> ldadd = new MutableLiveData<>();

    public FleetDetailsModel(@NonNull Application application) {
        super(application);
    }

    public void gettMotorcadeDetail(String no) {
        //车队详情
        NewAPIManager.getInstance().getLogisticsAPI().getMotorcadeDetail(no).enqueue(new Callback<BaseEntity<TeamRespVoEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<TeamRespVoEntity>> call, Response<BaseEntity<TeamRespVoEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null) {
                    return;
                }
                if (response.body().getData() == null) {
                    ldlist.postValue(new TeamRespVoEntity());
                    return;
                }
                if (response.body().getState().equals("ok")) {
                    ldlist.postValue(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<TeamRespVoEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    public void addOrUpdateAnchor(Context context,TeamRespVoEntity teamRespVoEntity){
        Map<String , Object> map = new ArrayMap<>();
        map.put("id",teamRespVoEntity.getId());
        map.put("motorcadeName",teamRespVoEntity.getName());
        map.put("motorcadeNo",teamRespVoEntity.getNo());
        map.put("name",teamRespVoEntity.getContact());
        map.put("telphone",teamRespVoEntity.getPhone());
        NewAPIManager.getInstance().getLogisticsAPI().addOrUpdateAnchor(map).enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                if (response.body()!=null){
                    if ("200".equals(response.body().getCode())){
                        EventBus.getDefault().post(GoodsManageEvent.make().setMessageTag(EventConstant.EVENT_REFRESH_AFFILIATED_SUCCESS));
                        ldadd.postValue("success");
                    }else {
                        MToast.showToast(context,"操作失败，请稍后重试");
                    }
                }else {
                    MToast.showToast(context,"操作失败，请稍后重试");
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                ldadd.postValue(new String());
                MToast.showToast(context,"操作失败，请稍后重试");
            }
        });
    }
}
